/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package gui;

import java.util.ArrayList;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import phys.CollisionResolver;
import vec.Vector3f;
import vec.Vector3f;

/**
 *
 * @author Vic
 */
public class MainFrame extends javax.swing.JFrame {

    /**
     * Creates new form MainFrame
     */
    public MainFrame() {
        initComponents();
        //setBackground(new Color(255,255,255,0));
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        physicsPanel = new gui.PhysicsPanel();
        timeSlider = new javax.swing.JSlider();
        jLabel1 = new javax.swing.JLabel();
        timeLabel = new javax.swing.JLabel();
        instructions = new javax.swing.JScrollPane();
        instructionPane = new javax.swing.JTextArea();
        collisionButton = new javax.swing.JToggleButton();
        jMenuBar1 = new javax.swing.JMenuBar();
        jMenu1 = new javax.swing.JMenu();
        jMenuItem1 = new javax.swing.JMenuItem();
        jMenu2 = new javax.swing.JMenu();
        jMenuItem2 = new javax.swing.JMenuItem();
        resetBoxButton = new javax.swing.JMenuItem();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("Spring");
        setBackground(new java.awt.Color(255, 255, 255));
        setForeground(java.awt.SystemColor.window);
        setLocationByPlatform(true);

        physicsPanel.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
        physicsPanel.setPreferredSize(new java.awt.Dimension(800, 800));

        javax.swing.GroupLayout physicsPanelLayout = new javax.swing.GroupLayout(physicsPanel);
        physicsPanel.setLayout(physicsPanelLayout);
        physicsPanelLayout.setHorizontalGroup(
            physicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 796, Short.MAX_VALUE)
        );
        physicsPanelLayout.setVerticalGroup(
            physicsPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 796, Short.MAX_VALUE)
        );

        timeSlider.addChangeListener(new javax.swing.event.ChangeListener() {
            public void stateChanged(javax.swing.event.ChangeEvent evt) {
                timeSliderStateChanged(evt);
            }
        });

        jLabel1.setText("Speed");

        timeLabel.setText("1.0");

        instructionPane.setBackground(new java.awt.Color(204, 204, 204));
        instructionPane.setColumns(20);
        instructionPane.setRows(5);
        instructionPane.setText("LMB = Move elements\nRMB = Add Spring\nCtrl+RMB = add anti-gravity\nAlt+RMB = add gravity\nRMB on element= remove element");
        instructionPane.setDisabledTextColor(new java.awt.Color(0, 0, 0));
        instructionPane.setEnabled(false);
        instructions.setViewportView(instructionPane);

        collisionButton.setText("Collision");
        collisionButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                collisionButtonActionPerformed(evt);
            }
        });

        jMenuBar1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mousePressed(java.awt.event.MouseEvent evt) {
                jMenuBar1MousePressed(evt);
            }
        });
        jMenuBar1.addMouseMotionListener(new java.awt.event.MouseMotionAdapter() {
            public void mouseDragged(java.awt.event.MouseEvent evt) {
                jMenuBar1MouseDragged(evt);
            }
        });

        jMenu1.setText("File");

        jMenuItem1.setText("Exit");
        jMenuItem1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem1ActionPerformed(evt);
            }
        });
        jMenu1.add(jMenuItem1);

        jMenuBar1.add(jMenu1);

        jMenu2.setText("Edit");

        jMenuItem2.setText("reset ball");
        jMenuItem2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jMenuItem2ActionPerformed(evt);
            }
        });
        jMenu2.add(jMenuItem2);

        resetBoxButton.setText("reset boxes");
        resetBoxButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                resetBoxButtonActionPerformed(evt);
            }
        });
        jMenu2.add(resetBoxButton);

        jMenuBar1.add(jMenu2);

        setJMenuBar(jMenuBar1);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(physicsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                        .addComponent(timeSlider, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                        .addGroup(layout.createSequentialGroup()
                            .addComponent(jLabel1)
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(timeLabel)))
                    .addComponent(instructions, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(collisionButton))
                .addContainerGap(18, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(physicsPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(0, 6, Short.MAX_VALUE))
            .addGroup(layout.createSequentialGroup()
                .addGap(8, 8, 8)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(timeLabel))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(timeSlider, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(collisionButton)
                .addGap(34, 34, 34)
                .addComponent(instructions, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jMenuItem1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem1ActionPerformed
        System.exit(0);
    }//GEN-LAST:event_jMenuItem1ActionPerformed

    private void timeSliderStateChanged(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_timeSliderStateChanged
        String text = String.valueOf(Math.pow(timeSlider.getValue() / 50.0, 1.5));
        if (text.length() > 5) {
            text = text.substring(0, 5);
        }
        timeLabel.setText(text);
    }//GEN-LAST:event_timeSliderStateChanged

    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jMenuItem2ActionPerformed
        synchronized (physicsPanel.ball.getState()) {
            physicsPanel.ball.getState().setVelocity( new Vector3f());
            physicsPanel.ball.getState().setPosition(new Vector3f((Vector3f) origen.clone()));
        }
    }//GEN-LAST:event_jMenuItem2ActionPerformed

    private void jMenuBar1MouseDragged(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuBar1MouseDragged
//        setLocation((int) (evt.getXOnScreen() - start.x), (int) (evt.getYOnScreen() - start.y));
    }//GEN-LAST:event_jMenuBar1MouseDragged

    private void jMenuBar1MousePressed(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jMenuBar1MousePressed
//
//        if (evt.getButton() == MouseEvent.BUTTON1 && evt.getClickCount() == 2) {
//            if (maximized) {
//                setSize(dim);
//                setLocation(evt.getXOnScreen() - (int) (start.x), evt.getYOnScreen() - (int) (start.y));
//                maximized = false;
//                System.out.println("restoring");
//            } else {
//                maximized = true;
//                dim = getSize();
//                setExtendedState(getState() | JFrame.MAXIMIZED_BOTH);
//                System.out.println("maximizing");
//                start.x = evt.getXOnScreen();
//                start.y = evt.getYOnScreen();
//                setLocation(0, 0);
//            }
//
//        } else {
//            start.x = evt.getX();
//            start.y = evt.getY();
//        }
    }//GEN-LAST:event_jMenuBar1MousePressed

    private void collisionButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_collisionButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_collisionButtonActionPerformed

    private void resetBoxButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_resetBoxButtonActionPerformed
        try {
            CollisionResolver.removeAll(Class.forName("spring.Block"));
            ArrayList toRemove = new ArrayList();
            for(Object obj:physicsPanel.Elements)
            {
                if(obj instanceof spring.Block)
                {
                    toRemove.add(obj);
                }
            }
            physicsPanel.Elements.removeAll(toRemove);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }//GEN-LAST:event_resetBoxButtonActionPerformed
//    boolean maximized;
//    Dimension dim = new Dimension();
//    final Vector3f start = new Vector3f();

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(MainFrame.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

//        MainFrame frame = new MainFrame();
//        frame.show();
//        frame.setIgnoreRepaint(true);
//        
//        frame.createBufferStrategy(2);
//        BufferStrategy bufferStrategy = frame.getBufferStrategy();
//        
//        float startTime = System.nanoTime() / 1e9f;
//        float lastTime = 0;
//        float remaining = 0;
//        final float dt = 1 / 600f;
//        
//        Insets insets = frame.getInsets();
//        System.out.println("insets = " + insets);
//        Rectangle clip = new Rectangle();
        try {
            PhysicsPanel.Loop();
        } catch (InterruptedException ex) {
            Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
    static int x = 0, y = 0;
    final static AtomicBoolean adjusting = new AtomicBoolean();
    int button = 0;
    final static Vector3f origen = new Vector3f(400, 400, 0);
//    static final Ball ball = new Ball(origen, new Rectangle((int) (origen.x - 800), (int) (origen.y - 800), 800, 800));
    final static ArrayList forces = new ArrayList<>();
    // Variables declaration - do not modify//GEN-BEGIN:variables
    javax.swing.JToggleButton collisionButton;
    private javax.swing.JTextArea instructionPane;
    private javax.swing.JScrollPane instructions;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JMenu jMenu1;
    private javax.swing.JMenu jMenu2;
    private javax.swing.JMenuBar jMenuBar1;
    private javax.swing.JMenuItem jMenuItem1;
    private javax.swing.JMenuItem jMenuItem2;
    public gui.PhysicsPanel physicsPanel;
    private javax.swing.JMenuItem resetBoxButton;
    private javax.swing.JLabel timeLabel;
    public javax.swing.JSlider timeSlider;
    // End of variables declaration//GEN-END:variables
}
